package tongcheng.com.tongcheng.ui.yemian.login.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.yanzhenjie.nohttp.RequestMethod;

import butterknife.Bind;
import tongcheng.com.tongcheng.ConStans;
import tongcheng.com.tongcheng.R;
import tongcheng.com.tongcheng.base.BaseActivity;
import tongcheng.com.tongcheng.bean.StatusBean;
import tongcheng.com.tongcheng.network.CallServer;
import tongcheng.com.tongcheng.network.MyRequest;
import tongcheng.com.tongcheng.network.OnCallBack;
import tongcheng.com.tongcheng.utils.GsonUtil;
import tongcheng.com.tongcheng.utils.ShowToast;
import tongcheng.com.tongcheng.utils.TimeCount;

public class ZhuceActivity extends BaseActivity {


    @Bind(R.id.zhuce_back)
    ImageView mZhuceBack;
    @Bind(R.id.zhuce_icon)
    ImageView mZhuceIcon;
    @Bind(R.id.zhuce_phone)
    EditText mZhucePhone;
    @Bind(R.id.zhuce_password)
    EditText mZhucePassword;
    @Bind(R.id.zhuce_yanzhen)
    EditText mZhuceYanzhen;
    @Bind(R.id.zhuce_send)
    TextView mZhuceSend;
    @Bind(R.id.zhuce_login)
    TextView mZhuceLogin;
    @Bind(R.id.zhuce_zhuce)
    Button mZhuceZhuce;
    private TimeCount time;

    @Override
    public Integer initResid() {
        return R.layout.activity_zhuce;
    }

    @Override
    public void initCreate(Bundle savedInstanceState) {

        mZhuceBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        time = new TimeCount(60000, 1000,mZhuceSend);
        mZhuceSend.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (TextUtils.isEmpty(mZhucePhone.getText().toString().trim())) {
                    ShowToast.showToast("手机号码不能为空");
                } else {
                    time.start();
                    sendCode();
                }

            }
        });

        mZhuceLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(ZhuceActivity.this,LoginActivity.class));
                finish();
            }
        });

        mZhuceZhuce.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                register();
            }
        });
    }

    private void register() {
        MyRequest request = new MyRequest(ConStans.RegisterSend_url, RequestMethod.POST);
        request.add("type", 2);
        request.add("phone", mZhucePhone.getText().toString().trim());
        request.add("password", mZhucePassword.getText().toString().trim());
        request.add("passwords", mZhucePassword.getText().toString().trim());
        request.add("code", mZhuceYanzhen.getText().toString().trim());
        CallServer.getInstance().addPost(this,request, 1, new OnCallBack() {
            @Override
            public void OnSuccess(int what, String json, Object ject) {

                StatusBean statusBean = GsonUtil.GsonToBean(json, StatusBean.class);
                if (statusBean != null) {
                    if (statusBean.status == 1) {
                        startactivity(ZhuceActivity.this, LoginActivity.class);
                        finish();
                    }
                    if (!TextUtils.isEmpty(statusBean.msg)) {
                        ShowToast.showToast(statusBean.msg);
                    }


                }
            }
        });
    }
    //发送验证码
    private void sendCode() {
        MyRequest request = new MyRequest(ConStans.RegisterSend_url, RequestMethod.POST);
        request.add("type", 1);
        request.add("phone", mZhucePhone.getText().toString().trim());
        CallServer.getInstance().addPost(this,request, 1, new OnCallBack() {
            @Override
            public void OnSuccess(int what, String json, Object ject) {

                StatusBean statusBean = GsonUtil.GsonToBean(json, StatusBean.class);
                if (statusBean != null) {
                    if (!TextUtils.isEmpty(statusBean.msg)) {
                        ShowToast.showToast(statusBean.msg);
                    }
                }
            }
        });
    }


}
